package serverside;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Scanner;

public class RecordDatabase {

	public static final int PATIENT = 1;
	public static final int NURSE = 2;
	public static final int DIVISION = 3;

	String path;

	public RecordDatabase(String path) {
		this.path = path;
	}

	/**
	 * Get records associated with a specific unit. It can be either a division, a nurse or a patient
	 * @param type - Use constants PATIENT, DIVISION or NURSE.
	 * @param name - Name of a unit
	 * @return records
	 */
	public String getRecords(int type,String name) {
		String result = "";
		switch(type) {
		case PATIENT:
			result = getPatientRecords(name);
			break;
		case NURSE:
			result = getNurseRecords(name);
			break;
		case DIVISION:
			result = getDivisionRecords(name);
			break;
		}
		return result;
	}

	/**
	 * Get all available records.
	 * @return
	 */
	public String getAllRecords() {
		File file = new File(path);
		Scanner sc = null;

		try {
			sc = new Scanner(file);
		} catch (FileNotFoundException e) {
			System.err.println("Database missing at: " + path);
			System.exit(1);
		}

		StringBuilder result = new StringBuilder(300);
		while(sc.hasNext()) {
			String line = sc.nextLine();

			if(line.trim().isEmpty()) {
				continue;
			}

			result.append(line);
			result.append("\n");

		}
		sc.close();
		return result.toString();
	}

	private String getDivisionRecords(String name) {
		File file = new File(path);
		Scanner sc = null;

		try {
			sc = new Scanner(file);
		} catch (FileNotFoundException e) {
			System.err.println("Database missing at: " + path);
			System.exit(1);
		}

		StringBuilder result = new StringBuilder(300);
		while(sc.hasNext()) {
			String line = sc.nextLine();

			if(line.trim().isEmpty()) {
				continue;
			}

			String[] data = line.split("; ");
			if(data[4].toLowerCase().equals(name.toLowerCase())) {
				result.append(line);
				result.append("\n");
			}

		}
		sc.close();
		return result.toString();
	}

	/**
	 * Get records of a patient if associated with a nurse or the nurse's division
	 * @param patient
	 * @param nurse
	 * @param division
	 * @return
	 */
	public String getRecords(String patient, String nurse, String division){
		File file = new File(path);
		Scanner sc = null;

		try {
			sc = new Scanner(file);
		} catch (FileNotFoundException e) {
			System.err.println("Database missing at: " + path);
			System.exit(1);
		}

		StringBuilder result = new StringBuilder(300);
		while(sc.hasNext()) {
			String line = sc.nextLine();

			if(line.trim().isEmpty()) {
				continue;
			}

			String[] data = line.split("; ");

			if(data[1].toLowerCase().equals(patient.toLowerCase()) && data[3].toLowerCase().equals(nurse.toLowerCase())
					|| data[1].toLowerCase().equals(patient.toLowerCase()) && data[4].toLowerCase().equals(division.toLowerCase())) {
				result.append(line);
				result.append("\n");
			}
		}
		sc.close();
		return result.toString();
	}

	private String getNurseRecords(String name) {
		File file = new File(path);
		Scanner sc = null;

		try {
			sc = new Scanner(file);
		} catch (FileNotFoundException e) {
			System.err.println("Database missing at: " + path);
			System.exit(1);
		}

		StringBuilder result = new StringBuilder(300);
		while(sc.hasNext()) {
			String line = sc.nextLine();

			if(line.trim().isEmpty()) {
				continue;
			}

			String[] data = line.split("; ");

			if(data[3].toLowerCase().equals(name.toLowerCase())) {
				result.append(line);
				result.append("\n");
			}	
		}
		sc.close();
		return result.toString();
	}

	/**
	 * @param name
	 * @return
	 */
	private String getPatientRecords(String name) {
		File file = new File(path);
		Scanner sc = null;

		try {
			sc = new Scanner(file);
		} catch (FileNotFoundException e) {
			System.err.println("Database missing at: " + path);
			System.exit(1);
		}

		StringBuilder result = new StringBuilder(300);
		while(sc.hasNext()) {
			String line = sc.nextLine();

			if(line.trim().isEmpty()) {
				continue;
			}

			String[] data = line.split("; ");
			if(data[1].toLowerCase().equals(name.toLowerCase())) {
				result.append(line);
				result.append("\n");
			}

		}
		sc.close();
		return result.toString();
	}

	public boolean addRecord(String patient, String doctor, String nurse, String division, String comment) {
		String records = getPatientRecords(patient);
		int index = 1;
		if(!records.equals("")) 
			index = records.split("\n").length + 1;
		PrintWriter outfile = null; 
		try {
			outfile = new PrintWriter(new FileWriter(path, true));
		} catch (IOException e) {
			System.err.println("Database not found at: " + path);
			return false;
		}
		String out = index + "; " + patient + "; " + doctor 
		+ "; " + nurse + "; " + division + "; " + comment + "\n";
		outfile.write(out);
		outfile.flush();
		outfile.close();
		return true;
	}

	/**
	 * Remove a record i from patient name.
	 * i = -1 removes all records.
	 * @param i
	 * @param name
	 * @return
	 */
	public boolean removeRecord(int i, String name) {
		String[] lines = getAllRecords().split("\n");
		StringBuilder out = new StringBuilder();
		if(i != -1)
			for(String l : lines) {
				String[] data = l.split("; ");
				if(!data[0].equals(String.valueOf(i)) || !data[1].toLowerCase().equals(name.toLowerCase())) {
					out.append(l);
					out.append("\n");
					System.out.println(out.toString());
				}
			}
		else 
			for(String l : lines) {
				String[] data = l.split("; ");
				if(!data[1].toLowerCase().equals(name.toLowerCase())) {
					out.append(l);
					out.append("\n");
					System.out.println(out.toString());
				}
			}
		PrintWriter outfile = null; 
		try {
			outfile = new PrintWriter(new FileWriter(path, false));
		} catch (IOException e) {
			System.err.println("Database not found at: " + path);
			return false;
		}
		outfile.write(out.toString());
		outfile.flush();
		outfile.close();
		return true;
	}

}
